/*
 * @Author: lijunqing
 * @Date: 2024-04-22 14:47:08
 * @LastEditors: lijunqing
 * @LastEditTime: 2024-04-22 15:17:21
 * @Description:
 */
const experss = require("express");

const app = experss();

app.use((req, res, next) => {
  req.on("data", (data) => {
    console.log(data.toString());
  });
  next();
});
app.use("/login", (req, res) => {
  let isLogin = false;
  req.on("data", (data) => {
    const str = data.toString();
    const userInfo = JSON.parse(str);

    if (userInfo.name == "admin") {
      isLogin = true;
    }
  });
  req.on("end", () => {
    const msg = isLogin
      ? "登录成功, 欢迎回来~'"
      : "登录失败, 请检测账号和密码是否正确~";

    res.end(msg);
  });
});
// 案例二: 注册用户的请求处理 /register post => username/password
app.post("/register", (req, res, next) => {
  // 1.获取本次请求过程中传递过来的json数据
  let isRegister = false;
  req.on("data", (data) => {
    const dataString = data.toString();
    const dataInfo = JSON.parse(dataString);
    // 查询数据库中该用户是否已经注册过
    isRegister = false;
  });

  req.on("end", () => {
    if (isRegister) {
      res.end("注册成功, 开始你的旅程~");
    } else {
      res.end("注册失败, 您输入的用户名被注册~");
    }
  });
});

app.listen("3000", () => {
  console.log("启动服务");
});
